package goleetcode

func minCost(nums []int, x int) int64 {
	n := len(nums)
	minValue := make([]int, n)
	copy(minValue, nums)
	res := sum(minValue)
	for i := 1; i < n; i++ {
		rotate := append(nums[n-i:], nums[:n-i]...)
		for j := range minValue {
			minValue[j] = int(min(float64(minValue[j]), float64(rotate[j])))
		}
		tmp := sum(minValue) + x * i
		if tmp < res {
			res = tmp
		}
	}
	return int64(res)
}

func sum(nums []int) int {
	sum := 0
	for _, num := range nums {
		sum += num
	}
	return sum
}